package com.cheneyjoo.impl;

import com.alibaba.fastjson.JSON;
import com.cheneyjoo.dao.ExamDao;
import com.cheneyjoo.model.Exam;
import com.cheneyjoo.model.Question;
import com.cheneyjoo.model.ResponseMessage;
import com.cheneyjoo.services.IExamService;

import java.io.IOException;
import java.util.List;
import java.util.Optional;

public class ExamServiceImpl implements IExamService {
    private ExamDao examDao;

    public ExamServiceImpl() {
        try {
            examDao = new ExamDao();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 添加试卷
     *
     * @param data
     * @return
     */
    @Override
    public ResponseMessage addExam(String data) {
        Exam exam = JSON.parseObject(data, Exam.class);
        boolean b = examDao.addExam(exam);
        return b ? new ResponseMessage<>(200, "添加成功", null) : new ResponseMessage<>(201, "添加失败", null);
    }

    /**
     * 修改试卷
     *
     * @param data
     * @return
     */
    @Override
    public ResponseMessage updateExam(String data) {
        Exam exam = JSON.parseObject(data, Exam.class);
        boolean b = examDao.updateExam(exam);
        return b ? new ResponseMessage<>(200, "修改成功", null) : new ResponseMessage<>(201, "修改失败", null);
    }

    /**
     * 删除试卷
     *
     * @param id
     * @return
     */
    @Override
    public ResponseMessage deleteExam(int id) {
        boolean b = examDao.deleteExam(id);
        return b ? new ResponseMessage<>(200, "删除成功", null) : new ResponseMessage<>(201, "删除失败", null);
    }

    /**
     * 获取全部试卷
     *
     * @return
     */
    @Override
    public ResponseMessage getExam() {
        List<Exam> examList = examDao.getExamList();
        return new ResponseMessage<>(200, "获取成功", examList);
    }

    /**
     * 根据ID获取试卷信息
     *
     * @param id
     * @return
     */
    @Override
    public ResponseMessage getExamById(int id) {
        Exam exam = examDao.getExam(id);
        return new ResponseMessage<>(200, "获取成功", exam);
    }

    /**
     * 发布试卷
     *
     * @param id
     * @return
     */
    @Override
    public ResponseMessage releaseExam(int id) {
        boolean b = examDao.updateStatus(id, 1);
        return b ? new ResponseMessage<>(200, "发布成功", null) : new ResponseMessage<>(201, "发布失败,指定的试卷不存在", null);
    }

    /**
     * 回收试卷
     *
     * @param id
     * @return
     */
    @Override
    public ResponseMessage recoveryExam(int id) {
        boolean b = examDao.updateStatus(id, 0);
        return b ? new ResponseMessage<>(200, "回收成功", null) : new ResponseMessage<>(201, "回收失败,指定的试卷不存在", null);
    }

    /**
     * 根据状态获取考试信息
     * @param status
     * @return
     */
    @Override
    public ResponseMessage getExamByStatus(int status) {
        List<Exam> examList=examDao.getExamByStatus(status);
        return new ResponseMessage<>(200, "获取成功", examList) ;
    }

    @Override
    public ResponseMessage getQuestion(int examID, int questionID) {
        Question question = examDao.getQuestion(examID, questionID);
        return Optional.ofNullable(question).isPresent() ? new ResponseMessage<>(200, "获取成功", question) : new ResponseMessage<Question>(201, "获取失败", null);
    }

    /**
     * 获取试题列表
     *
     * @param id
     * @return
     */
    @Override
    public ResponseMessage getQuestions(int id) {
        List<Question> questions = examDao.getQuestions(id);
        return new ResponseMessage<>(200, "获取成功", questions);
    }

    /**
     * 更新试题信息
     *
     * @param examID
     * @param questionID
     * @param data
     * @return
     */
    @Override
    public ResponseMessage updateQuestion(int examID, int questionID, String data) {
        Question question = JSON.parseObject(data, Question.class);
        boolean b = examDao.updateQuestion(examID, questionID, question);
        return b ? new ResponseMessage<>(200, "修改成功", null) : new ResponseMessage<>(201, "修改失败", null);
    }

    /**
     * 删除试题信息
     * @param examID
     * @param questionID
     * @return
     */
    @Override
    public ResponseMessage deleteQuestion(int examID, int questionID) {
        boolean b = examDao.deleteQuestion(examID, questionID);
        return b ? new ResponseMessage<>(200, "删除成功", null) : new ResponseMessage<>(201, "删除失败", null);
    }
}
